package controller.SRV;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.ejb.EJB;

import controller.SRV.*;
import entity.MDoctor;
import entity.SrcCureplanDoctor;
import entity.SrvCureplan;
import session.SrvCureserviceFacade;
import session.SrvCureplanFacade;
import session.SrvCurePlanManager;
import util.jQueryForm.jQueryFormFormatterUtil;
import util.jQueryForm.jQueryFormSelector;

@WebServlet(name = "SRV_CureplanDetail",
loadOnStartup = 1,
urlPatterns = {"/SRV/CureplanDetail",
    "/SRV/CureplanDetail/formCureplan_Submit",
    "/SRV/CureplanDetail/getJson"})
public class SRV_CureplanDetail extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="Init Param">
    private String sampleServletContext;
    @EJB
    private SrvCureserviceFacade srvCureserviceFacade;
    @EJB
    private SrvCurePlanManager srvCurePlanManager;
    @EJB
    private SrvCureplanFacade srvCureplanFacade;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        super.init(servletConfig);

        // initialize servlet with configuration information
        sampleServletContext = servletConfig.getServletContext().getInitParameter("sampleServletContext");
    }
    // </editor-fold>

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // <editor-fold defaultstate="collapsed" desc="Default Page Param">
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        response.setContentType("text/json;charset=utf-8");
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Control Page Redirect & Reset Requset-Attribute">
        if (userPath.equals("/SRV/CureplanDetail")) {
            // <editor-fold defaultstate="collapsed" desc="URL Param">
            String cureServiceId = (request.getParameter("cureServiceId") != null) ? request.getParameter("cureServiceId").trim() : "";
            String curePlanId = (request.getParameter("curePlanId") != null) ? request.getParameter("curePlanId").trim() : "";
            if (!curePlanId.isEmpty()) {
                cureServiceId = srvCureplanFacade.find(Long.parseLong(curePlanId)).getSrvCureservice().getId().toString();
            }
            String fromPage = (request.getParameter("fromPage") != null) ? request.getParameter("fromPage").trim() : "";
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Attribute Param">
            request.setAttribute("cureServiceId", cureServiceId);
            request.setAttribute("curePlanId", curePlanId);
            request.setAttribute("fromPage", fromPage);
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            userPath = "/SRV/CureplanDetail";
            isDispatcher = true;
            // </editor-fold>
        }
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Change Page">
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            //@TODO : Error Code Here
            ex.printStackTrace();
        }
        // </editor-fold>
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // <editor-fold defaultstate="collapsed" desc="Default Page Param">
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ

        //if set=>text/json firefox error
        //if only set response.setCharacterEncoding IE error must set charset=utf-8 in setContentType
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;
        String strJson = "{}";
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Request Function">
        //====Load Data For Page Load====
        if (userPath.equals("/SRV/CureplanDetail/getJson")) {
            // <editor-fold defaultstate="collapsed" desc="Param">
            response.setContentType("text/json;charset=utf-8");
            String curePlanId = (request.getParameter("curePlanId") != null) ? request.getParameter("curePlanId").trim() : "";
            
            SrvCureplan srvCurePlan;
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Coding">
            if (!curePlanId.isEmpty()) {
                srvCurePlan = srvCureplanFacade.find(Long.parseLong(curePlanId));

                if (srvCurePlan != null) {
                    try {
                        // <editor-fold defaultstate="collapsed" desc="Bind Object To Json Data">
                        out = response.getWriter();
                        strJson = DisplayData(srvCurePlan);
                        out.println(strJson);
                        // </editor-fold>
                    } catch (Exception ex) {
                        // <editor-fold defaultstate="collapsed" desc="Popup Message">

                        // </editor-fold>
                        //@TODO : Implement Error function to show on front
                        System.out.println("Error message" + ex.getMessage());
                    } finally {
                        // <editor-fold defaultstate="collapsed" desc="Clear Value">
                        srvCurePlan = null;
                        out = null;
                        strJson = "";
                        // </editor-fold>
                        //@TODO : Implement message function to show on front
                    }
                }
            } else { //when curePlanId is null or empty donothing
                out = response.getWriter();
                out.write(strJson);
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            userPath = "/SRV/CureplanDetail";
            isDispatcher = false;
            // </editor-fold>
        } //====Cureplan Form Submit====
        else if (userPath.equals("/SRV/CureplanDetail/formCureplan_Submit")) {
            // <editor-fold defaultstate="collapsed" desc="URL Param">
            //Form Serialize Param
            String txtSymptom = (request.getParameter("txtSymptom") != null) ? request.getParameter("txtSymptom").trim() : "";
            String txtAllergicHistory = (request.getParameter("txtAllergicHistory") != null) ? request.getParameter("txtAllergicHistory").trim() : "";
            String txtDiseaseHistory = (request.getParameter("txtDiseaseHistory") != null) ? request.getParameter("txtDiseaseHistory").trim() : "";
            String uplAttachHistory = (request.getParameter("uplAttachHistory") != null) ? request.getParameter("uplAttachHistory").trim() : "";
            String txtRoomType = (request.getParameter("txtRoomType") != null) ? request.getParameter("txtRoomType").trim() : "";
            String txtPriceMinAccept = (request.getParameter("txtPriceMinAccept") != null) ? request.getParameter("txtPriceMinAccept").trim() : "";
            String txtPriceMaxAccept = (request.getParameter("txtPriceMaxAccept") != null) ? request.getParameter("txtPriceMaxAccept").trim() : "";
            String datDateMinAccept = (request.getParameter("datDateMinAccept") != null) ? request.getParameter("datDateMinAccept").trim() : "";
            String datDateMaxAccept = (request.getParameter("datDateMaxAccept") != null) ? request.getParameter("datDateMaxAccept").trim() : "";
            String chkRequestQuotation = (request.getParameter("chkRequestQuotation") != null) ? request.getParameter("chkRequestQuotation").trim() : "";
            String ddlHotel = (request.getParameter("ddlHotel") != null) ? request.getParameter("ddlHotel").trim() : "";
            String ddlSpecialist = (request.getParameter("ddlSpecialist") != null) ? request.getParameter("ddlSpecialist").trim() : "";
            //Hidden field
            String cureServiceId = (request.getParameter("cureServiceId") != null) ? request.getParameter("cureServiceId").trim() : "";
            String curePlanId = (request.getParameter("curePlanId") != null) ? request.getParameter("curePlanId").trim() : "";
            String fromPage = (request.getParameter("fromPage") != null) ? request.getParameter("fromPage").trim() : "";
            //Specify Submit button
            String btnSubmit = (request.getParameter("btnSubmit") != null) ? request.getParameter("btnSubmit").trim() : "";
            String btnSaveDraft = (request.getParameter("btnSaveDraft") != null) ? request.getParameter("btnSaveDraft").trim() : "";
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Param">
            //if set=>text/json firefox error
            //if only set response.setCharacterEncoding ie error must set charset=utf-8 in setContentType
            response.setContentType("text/html;charset=utf-8");
            SrvCureplan srvCurePlan;
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Coding">
            try {
                //@TODO : Validate Here (Server Side function)
                // <editor-fold defaultstate="collapsed" desc="Validate Data">

                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Do DB Transaction">
                srvCurePlan = srvCurePlanManager.SaveCurePlanDetail(txtSymptom, txtAllergicHistory, txtDiseaseHistory, uplAttachHistory, txtRoomType, txtPriceMinAccept, txtPriceMaxAccept, datDateMinAccept, datDateMaxAccept, chkRequestQuotation, ddlHotel, ddlSpecialist, cureServiceId, curePlanId, btnSubmit, btnSaveDraft);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Object To Json Data">
                out = response.getWriter();
                strJson = DisplayData(srvCurePlan);
                out.println(strJson);
                // </editor-fold>
            } catch (Exception ex) {
                // <editor-fold defaultstate="collapsed" desc="Popup Message">

                // </editor-fold>
                //@TODO : Implement Error function to show on front
                System.out.println("Error message" + ex.getMessage());
            } finally {
                // <editor-fold defaultstate="collapsed" desc="Clear Value">
                srvCurePlan = null;
                out = null;
                strJson = "";
                // </editor-fold>
                //@TODO : Implement message function to show on front
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Change Page Param">
            userPath = "/SRV/CureplanDetail";
            isDispatcher = false;
            // </editor-fold>
        }
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Change Page">
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            //@TODO : Error Code Here
            ex.printStackTrace();
        }
        // </editor-fold>
    }

    // <editor-fold defaultstate="collapsed" desc="Function">
    public String DisplayData(SrvCureplan srvCurePlan) {

        // <editor-fold defaultstate="collapsed" desc="Param">
        jQueryFormFormatterUtil objJQueryFormFormatterUtil;
        String strJson = "{}";
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="UserControl Param">
        //@TODO : Make string Message class
        //Message Display after finish function
        String strMessage = "";

        //@TODO : Make Button class
        //Button Command
        String ButtonCommand_wrapper = "";
        String btnBack = "<button type=\"button\" name=\"btnBack\" id=\"btnBack\" value=\"Back\">"
                + "<span>Back</span>"
                + "</button>";
        String btnManagePlan = "<button type=\"button\" name=\"btnManagePlan\" id=\"btnManagePlan\" value=\"ManagePlan\">"
                + "<span>Manage Plan</span>"
                + "</button>";
        String btnReset = "<input type=\"reset\" name=\"btnReset\" id=\"btnReset\" value=\"Reset\" />";
        String btnSaveDraft = "<button type=\"submit\" name=\"btnSaveDraft\" id=\"btnSaveDraft\" value=\"SaveDraft\">"
                + "<span>Save Draft</span>"
                + "</button>";
        String btnSubmit = "<input type=\"submit\" name=\"btnSubmit\" id=\"btnSubmit\" value=\"Submit\" />";
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Mapping Param">
        //Support Jquery "Attribute" function
        Map<String, String> mapHtml = null;
        Map<String, String> mapText = null;
        Map<String, String> mapVal = null;
        Map<String, String> mapAddClass = null;
        Map<String, String> mapRemoveClass = null;
        Map<String, String> mapToggleClass = null;
        Map<String, String> mapShowHide = null;

        //Support Jquery "Attribute" function
        Map<String, String> mapAppend = null;
        Map<String, String> mapAppendTo = null;
        Map<String, String> mapReplaceWith = null;
        Map<String, String> mapReplaceAll = null;
        Map<String, String> mapRemove = null;
        Map<String, String> mapDetach = null;
        Map<String, String> mapEmpty = null;
        Map<String, String> mapClone = null;

        //Add/Remove Attribute To Selector
        List<jQueryFormSelector> mapAttrs = null;
        jQueryFormSelector attr = null;
        Map<String, String> mapAttrItem = null;
        Map<String, String> mapRemoveAttr = null;
        // </editor-fold>

        // <editor-fold defaultstate="collapsed" desc="Prepare DB value">
        //For srvCurePlan
        //ดึงข้อมูลผู้ป่วย [srvCurePlan]
        String strPriceMinAccept = (srvCurePlan.getPriceMinAccept() != null) ? srvCurePlan.getPriceMinAccept().toString() : "";
        String strPriceMaxAccept = (srvCurePlan.getPriceMaxAccept() != null) ? srvCurePlan.getPriceMaxAccept().toString() : "";

        DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
        String strDateMinAccept = (srvCurePlan.getDateMinAccept() != null) ? df.format(srvCurePlan.getDateMinAccept()) : "";
        String strDateMaxAccept = (srvCurePlan.getDateMaxAccept() != null) ? df.format(srvCurePlan.getDateMaxAccept()) : "";

        String strHotel = (srvCurePlan.getMHotel() != null) ? srvCurePlan.getMHotel().getName() : "";
        String strSpecialist = (srvCurePlan.getMSpecialist() != null) ? srvCurePlan.getMSpecialist().getName() : "";
        Long intHotel = (srvCurePlan.getMHotel() != null) ? srvCurePlan.getMHotel().getId() : 0;
        Long intSpecialist = (srvCurePlan.getMSpecialist() != null) ? srvCurePlan.getMSpecialist().getId() : 0;

        //For srvCurePlan_doctor
        String strSymptomComment = "";
        String strAllergicHistoryComment = "";
        String strDiseaseHistoryComment = "";
        String strAttachHistoryComment = "";
        String strRoomTypeComment = "";
        String strPriceMinAcceptComment = "";
        String strPriceMaxAcceptComment = "";
        String strDateMinAcceptComment = "";
        String strDateMaxAcceptComment = "";
        String strRequestQuotation = "";
        String strHotelComment = "";
        String strSpecialistComment = "";
        String strAcceptCondition = "";
        //ดึงข้อมูลคอมเม้นของหมอ [srvCurePlanDoctor]
        if (srvCurePlan.getSrcCureplanDoctorCollection() != null) {
            SrcCureplanDoctor objSrcCureplanDoctor = null;
            //want select first object because 1 to 1 relation
            for (SrcCureplanDoctor tmpSrcCureplanDoctor : srvCurePlan.getSrcCureplanDoctorCollection()) {
                objSrcCureplanDoctor = tmpSrcCureplanDoctor;
                break;
            }
            if (objSrcCureplanDoctor != null) {
                strSymptomComment = (objSrcCureplanDoctor.getSymptom() != null) ? objSrcCureplanDoctor.getSymptom() : "";
                strAllergicHistoryComment = (objSrcCureplanDoctor.getAllergicHistory() != null) ? objSrcCureplanDoctor.getAllergicHistory() : "";
                strDiseaseHistoryComment = (objSrcCureplanDoctor.getDiseaseHistory() != null) ? objSrcCureplanDoctor.getDiseaseHistory() : "";
                strAttachHistoryComment = (objSrcCureplanDoctor.getAttachHistoryComment() != null) ? objSrcCureplanDoctor.getAttachHistoryComment() : "";
                strRoomTypeComment = (objSrcCureplanDoctor.getRoomType() != null) ? objSrcCureplanDoctor.getRoomType() : "";
                strPriceMinAcceptComment = (objSrcCureplanDoctor.getPriceMinAccept() != null) ? objSrcCureplanDoctor.getPriceMinAccept() : "";
                strPriceMaxAcceptComment = (objSrcCureplanDoctor.getPriceMaxAccept() != null) ? objSrcCureplanDoctor.getPriceMaxAccept() : "";
                strDateMinAcceptComment = (objSrcCureplanDoctor.getDateMinAccept() != null) ? objSrcCureplanDoctor.getDateMinAccept() : "";
                strDateMaxAcceptComment = (objSrcCureplanDoctor.getDateMaxAccept() != null) ? objSrcCureplanDoctor.getDateMaxAccept() : "";
                strRequestQuotation = (objSrcCureplanDoctor.getAttachQuotation() != null) ? objSrcCureplanDoctor.getAttachQuotation() : "";
                strHotelComment = (objSrcCureplanDoctor.getMHotel() != null) ? objSrcCureplanDoctor.getMHotel().getName() : "";
                strSpecialistComment = (objSrcCureplanDoctor.getMSpecialist() != null) ? objSrcCureplanDoctor.getMSpecialist().getName() : "";
            }
        }
        // </editor-fold>

        try {
            // <editor-fold defaultstate="collapsed" desc="Data Bind">
            if (srvCurePlan.getStatus().equals("D")) { //สถานะ ร่าง แผนการนักหมาย
                //Set Message By Status
                strMessage = "<div class=\"notification-box\"><p class=\"icon\"><img src=\"http://osc4.template-help.com/wt_32136/images/icon-note.png\" alt=\"\"></p>You currently draft your plan.<br/>Press 'Submit' to send request to 'Doctor' or 'Hospital</div>";

                // <editor-fold defaultstate="collapsed" desc="Bind Html(innerHtml) : replace html to #'Selector'">
                mapHtml = new LinkedHashMap<String, String>();
                mapHtml.put("#message", strMessage);
                //For srvCurePlan
                mapHtml.put("#lblSymptom", srvCurePlan.getSymptom());
                mapHtml.put("#lblAllergicHistory", srvCurePlan.getAllergicHistory());
                mapHtml.put("#lblDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapHtml.put("#lblRoomType", srvCurePlan.getRoomType());
                mapHtml.put("#lblPriceMinAccept", strPriceMinAccept);
                mapHtml.put("#lblPriceMaxAccept", strPriceMaxAccept);
                mapHtml.put("#lblDateMinAccept", strDateMinAccept);
                mapHtml.put("#lblDateMaxAccept", strDateMaxAccept);
                mapHtml.put("#lblHotel", strHotel);
                mapHtml.put("#lblSpecialist", strSpecialist);
                //For srvCurePlan_doctor
                mapHtml.put("#lblSymptomComment", strSymptomComment);
                mapHtml.put("#lblAllergicHistoryComment", strAllergicHistoryComment);
                mapHtml.put("#lblDiseaseHistoryComment", strDiseaseHistoryComment);
                mapHtml.put("#lblAttachHistoryComment", strAttachHistoryComment);
                mapHtml.put("#lblRoomTypeComment", strRoomTypeComment);
                mapHtml.put("#lblPriceMinAcceptComment", strPriceMinAcceptComment);
                mapHtml.put("#lblPriceMaxAcceptComment", strPriceMaxAcceptComment);
                mapHtml.put("#lblDateMinAcceptComment", strDateMinAcceptComment);
                mapHtml.put("#lblDateMaxAcceptComment", strDateMaxAcceptComment);
                mapHtml.put("#lblRequestQuotation", strRequestQuotation);
                mapHtml.put("#lblHotelComment", strHotelComment);
                mapHtml.put("#lblSpecialistComment", strSpecialistComment);


                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Val : most use for bind value from DB to HtmlControl(.value)">
                mapVal = new LinkedHashMap<String, String>();
                //For Hidden Field
                mapVal.put("#curePlanId", srvCurePlan.getId().toString());
                mapVal.put("#cureServiceId", srvCurePlan.getSrvCureservice().getId().toString());
                //For srvCurePlan
                mapVal.put("#txtSymptom", srvCurePlan.getSymptom());
                mapVal.put("#txtAllergicHistory", srvCurePlan.getAllergicHistory());
                mapVal.put("#txtDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapVal.put("#txtRoomType", srvCurePlan.getRoomType());
                mapVal.put("#txtPriceMinAccept", strPriceMinAccept);
                mapVal.put("#txtPriceMaxAccept", strPriceMaxAccept);
                mapVal.put("#datDateMinAccept", strDateMinAccept);
                mapVal.put("#datDateMaxAccept", strDateMaxAccept);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Visible of Control : Show/Hide Control by '1' = show, '0' = hide">
                mapShowHide = new LinkedHashMap<String, String>();
                //Show Control
                mapShowHide.put("#txtSymptom", "1");
                mapShowHide.put("#txtAllergicHistory", "1");
                mapShowHide.put("#txtDiseaseHistory", "1");
                mapShowHide.put("#pnlAttachHistory", "1");
                mapShowHide.put("#txtRoomType", "1");
                mapShowHide.put("#txtPriceMinAccept", "1");
                mapShowHide.put("#txtPriceMaxAccept", "1");
                mapShowHide.put("#datDateMinAccept", "1");
                mapShowHide.put("#datDateMaxAccept", "1");
                mapShowHide.put("#ddlHotel", "1");
                mapShowHide.put("#ddlSpecialist", "1");
                //mapShowHide.put("#uniform-btnReset", "1"); //ใช้ Remove แทน
                //mapShowHide.put("#uniform-btnSaveDraft", "1"); //ใช้ Remove แทน
                //mapShowHide.put("#uniform-btnSubmit", "1"); //ใช้ Remove แทน
                //Hide Control
                mapShowHide.put("#lblSymptom", "0");
                mapShowHide.put("#lblAllergicHistory", "0");
                mapShowHide.put("#lblDiseaseHistory", "0");
                mapShowHide.put("#lblRoomType", "0");
                mapShowHide.put("#lblPriceMinAccept", "0");
                mapShowHide.put("#lblPriceMaxAccept", "0");
                mapShowHide.put("#lblDateMinAccept", "0");
                mapShowHide.put("#lblDateMaxAccept", "0");
                mapShowHide.put("#lblHotel", "0");
                mapShowHide.put("#lblSpecialist", "0");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove : Remove 'Selector' And Can make Condition">

                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove Attributes">
                mapRemoveAttr = new LinkedHashMap<String, String>();
                mapRemoveAttr.put("#chkRequestQuotation", "disabled");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Add attribute for control : use for attribute checked, disabled or else">
                //Add Attributes
                mapAttrs = new ArrayList<jQueryFormSelector>();
                attr = new jQueryFormSelector();
                attr.setId("#chkRequestQuotation");
                mapAttrItem = new LinkedHashMap<String, String>();
                if (srvCurePlan.getRequestQuotation() != null && srvCurePlan.getRequestQuotation() == true) {
                    mapAttrItem.put("checked", "checked");
                } else {
                    //Remove Attribute
                    mapRemoveAttr.put("#chkRequestQuotation", "checked");
                }
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlHotel option[value=" + intHotel + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlSpecialist option[value=" + intSpecialist + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);
                // </editor-fold>
            } else if (srvCurePlan.getStatus().equals("W")) { //สถานะ รอ การคอมเม้นจากแพทย์
                //Set Message By Status
                strMessage = "<div class=\"info-box\"><p class=\"icon\"><img src=\"http://osc4.template-help.com/wt_32136/images/icon-info.png\" alt=\"\"></p>Waiting 'Doctor' or 'Hospital' for accepting your plan.<br/><br/></div>";

                // <editor-fold defaultstate="collapsed" desc="Bind Html(innerHtml) : replace html to #'Selector'">
                mapHtml = new LinkedHashMap<String, String>();
                mapHtml.put("#message", strMessage);
                //For srvCurePlan
                mapHtml.put("#lblSymptom", srvCurePlan.getSymptom());
                mapHtml.put("#lblAllergicHistory", srvCurePlan.getAllergicHistory());
                mapHtml.put("#lblDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapHtml.put("#lblRoomType", srvCurePlan.getRoomType());
                mapHtml.put("#lblPriceMinAccept", strPriceMinAccept);
                mapHtml.put("#lblPriceMaxAccept", strPriceMaxAccept);
                mapHtml.put("#lblDateMinAccept", strDateMinAccept);
                mapHtml.put("#lblDateMaxAccept", strDateMaxAccept);
                mapHtml.put("#lblHotel", strHotel);
                mapHtml.put("#lblSpecialist", strSpecialist);
                //For srvCurePlan_doctor
                mapHtml.put("#lblSymptomComment", strSymptomComment);
                mapHtml.put("#lblAllergicHistoryComment", strAllergicHistoryComment);
                mapHtml.put("#lblDiseaseHistoryComment", strDiseaseHistoryComment);
                mapHtml.put("#lblAttachHistoryComment", strAttachHistoryComment);
                mapHtml.put("#lblRoomTypeComment", strRoomTypeComment);
                mapHtml.put("#lblPriceMinAcceptComment", strPriceMinAcceptComment);
                mapHtml.put("#lblPriceMaxAcceptComment", strPriceMaxAcceptComment);
                mapHtml.put("#lblDateMinAcceptComment", strDateMinAcceptComment);
                mapHtml.put("#lblDateMaxAcceptComment", strDateMaxAcceptComment);
                mapHtml.put("#lblRequestQuotation", strRequestQuotation);
                mapHtml.put("#lblHotelComment", strHotelComment);
                mapHtml.put("#lblSpecialistComment", strSpecialistComment);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Val : most use for bind value from DB to HtmlControl(.value)">
                mapVal = new LinkedHashMap<String, String>();
                //For Hidden Field
                mapVal.put("#curePlanId", srvCurePlan.getId().toString());
                mapVal.put("#cureServiceId", srvCurePlan.getSrvCureservice().getId().toString());
                //For srvCurePlan
                mapVal.put("#txtSymptom", srvCurePlan.getSymptom());
                mapVal.put("#txtAllergicHistory", srvCurePlan.getAllergicHistory());
                mapVal.put("#txtDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapVal.put("#txtRoomType", srvCurePlan.getRoomType());
                mapVal.put("#txtPriceMinAccept", strPriceMinAccept);
                mapVal.put("#txtPriceMaxAccept", strPriceMaxAccept);
                mapVal.put("#datDateMinAccept", strDateMinAccept);
                mapVal.put("#datDateMaxAccept", strDateMaxAccept);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Visible of Control : Show/Hide Control by '1' = show, '0' = hide">
                mapShowHide = new LinkedHashMap<String, String>();
                //Show Control
                mapShowHide.put("#lblSymptom", "1");
                mapShowHide.put("#lblAllergicHistory", "1");
                mapShowHide.put("#lblDiseaseHistory", "1");
                mapShowHide.put("#lblRoomType", "1");
                mapShowHide.put("#lblPriceMinAccept", "1");
                mapShowHide.put("#lblPriceMaxAccept", "1");
                mapShowHide.put("#lblDateMinAccept", "1");
                mapShowHide.put("#lblDateMaxAccept", "1");
                mapShowHide.put("#lblHotel", "1");
                mapShowHide.put("#lblSpecialist", "1");
                //Hide Control
                mapShowHide.put("#txtSymptom", "0");
                mapShowHide.put("#txtAllergicHistory", "0");
                mapShowHide.put("#txtDiseaseHistory", "0");
                mapShowHide.put("#pnlAttachHistory", "0");
                mapShowHide.put("#txtRoomType", "0");
                mapShowHide.put("#txtPriceMinAccept", "0");
                mapShowHide.put("#txtPriceMaxAccept", "0");
                mapShowHide.put("#datDateMinAccept", "0");
                mapShowHide.put("#datDateMaxAccept", "0");
                //mapShowHide.put("#ddlHotel", "0");
                //mapShowHide.put("#ddlSpecialist", "0");
                mapShowHide.put("#uniform-ddlHotel", "0");
                mapShowHide.put("#uniform-ddlSpecialist", "0");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove : Remove 'Selector' And Can make Condition">
                mapRemove = new LinkedHashMap<String, String>();
                mapRemove.put("#uniform-btnReset", "");
                mapRemove.put("#uniform-btnSaveDraft", "");
                mapRemove.put("#uniform-btnSubmit", "");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove Attributes">
                mapRemoveAttr = new LinkedHashMap<String, String>();
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Add attribute for control : use for attribute checked, disabled or else">
                //Add Attributes
                mapAttrs = new ArrayList<jQueryFormSelector>();
                attr = new jQueryFormSelector();
                attr.setId("#chkRequestQuotation");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("disabled", "disabled");
                if (srvCurePlan.getRequestQuotation() != null && srvCurePlan.getRequestQuotation() == true) {
                    mapAttrItem.put("checked", "checked");
                } else {
                    mapRemoveAttr.put("#chkRequestQuotation", "checked");
                }
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlHotel option[value=" + intHotel + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlSpecialist option[value=" + intSpecialist + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#chkAcceptCondition");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("disabled", "disabled");
                mapAttrItem.put("checked", "checked");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);
                // </editor-fold>

            } else if (srvCurePlan.getStatus().equals("E")) { //สถานะ แก้ไข รายละเอียดแผนการนัดหมาย
                //Set Message By Status
                strMessage = "<div class=\"error-box\"><p class=\"icon\"><img src=\"http://osc4.template-help.com/wt_32136/images/icon-error.png\" alt=\"\"></p>'Doctor' or 'Hospital' comment your plan. And request for change your plan.<br/></div>";

                // <editor-fold defaultstate="collapsed" desc="Bind Html(innerHtml) : replace html to #'Selector'">
                mapHtml = new LinkedHashMap<String, String>();
                mapHtml.put("#message", strMessage);
                //For srvCurePlan
                mapHtml.put("#lblSymptom", srvCurePlan.getSymptom());
                mapHtml.put("#lblAllergicHistory", srvCurePlan.getAllergicHistory());
                mapHtml.put("#lblDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapHtml.put("#lblRoomType", srvCurePlan.getRoomType());
                mapHtml.put("#lblPriceMinAccept", strPriceMinAccept);
                mapHtml.put("#lblPriceMaxAccept", strPriceMaxAccept);
                mapHtml.put("#lblDateMinAccept", strDateMinAccept);
                mapHtml.put("#lblDateMaxAccept", strDateMaxAccept);
                mapHtml.put("#lblHotel", strHotel);
                mapHtml.put("#lblSpecialist", strSpecialist);
                //For srvCurePlan_doctor
                mapHtml.put("#lblSymptomComment", strSymptomComment);
                mapHtml.put("#lblAllergicHistoryComment", strAllergicHistoryComment);
                mapHtml.put("#lblDiseaseHistoryComment", strDiseaseHistoryComment);
                mapHtml.put("#lblAttachHistoryComment", strAttachHistoryComment);
                mapHtml.put("#lblRoomTypeComment", strRoomTypeComment);
                mapHtml.put("#lblPriceMinAcceptComment", strPriceMinAcceptComment);
                mapHtml.put("#lblPriceMaxAcceptComment", strPriceMaxAcceptComment);
                mapHtml.put("#lblDateMinAcceptComment", strDateMinAcceptComment);
                mapHtml.put("#lblDateMaxAcceptComment", strDateMaxAcceptComment);
                mapHtml.put("#lblRequestQuotation", strRequestQuotation);
                mapHtml.put("#lblHotelComment", strHotelComment);
                mapHtml.put("#lblSpecialistComment", strSpecialistComment);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Val : most use for bind value from DB to HtmlControl(.value)">
                mapVal = new LinkedHashMap<String, String>();
                //For Hidden Field
                mapVal.put("#curePlanId", srvCurePlan.getId().toString());
                mapVal.put("#cureServiceId", srvCurePlan.getSrvCureservice().getId().toString());
                //For srvCurePlan
                mapVal.put("#txtSymptom", srvCurePlan.getSymptom());
                mapVal.put("#txtAllergicHistory", srvCurePlan.getAllergicHistory());
                mapVal.put("#txtDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapVal.put("#txtRoomType", srvCurePlan.getRoomType());
                mapVal.put("#txtPriceMinAccept", strPriceMinAccept);
                mapVal.put("#txtPriceMaxAccept", strPriceMaxAccept);
                mapVal.put("#datDateMinAccept", strDateMinAccept);
                mapVal.put("#datDateMaxAccept", strDateMaxAccept);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Visible of Control : Show/Hide Control by '1' = show, '0' = hide">
                mapShowHide = new LinkedHashMap<String, String>();
                //Show Control
                mapShowHide.put("#txtSymptom", "1");
                mapShowHide.put("#txtAllergicHistory", "1");
                mapShowHide.put("#txtDiseaseHistory", "1");
                mapShowHide.put("#pnlAttachHistory", "1");
                mapShowHide.put("#txtRoomType", "1");
                mapShowHide.put("#txtPriceMinAccept", "1");
                mapShowHide.put("#txtPriceMaxAccept", "1");
                mapShowHide.put("#datDateMinAccept", "1");
                mapShowHide.put("#datDateMaxAccept", "1");
                mapShowHide.put("#ddlHotel", "1");
                mapShowHide.put("#ddlSpecialist", "1");
                //Hide Control
                mapShowHide.put("#lblSymptom", "0");
                mapShowHide.put("#lblAllergicHistory", "0");
                mapShowHide.put("#lblDiseaseHistory", "0");
                mapShowHide.put("#lblRoomType", "0");
                mapShowHide.put("#lblPriceMinAccept", "0");
                mapShowHide.put("#lblPriceMaxAccept", "0");
                mapShowHide.put("#lblDateMinAccept", "0");
                mapShowHide.put("#lblDateMaxAccept", "0");
                mapShowHide.put("#lblHotel", "0");
                mapShowHide.put("#lblSpecialist", "0");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove : Remove 'Selector' And Can make Condition">

                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove Attributes">
                mapRemoveAttr = new LinkedHashMap<String, String>();
                mapRemoveAttr.put("#chkRequestQuotation", "disabled");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Add attribute for control : use for attribute checked, disabled or else">
                //Add Attributes
                mapAttrs = new ArrayList<jQueryFormSelector>();
                attr = new jQueryFormSelector();
                attr.setId("#chkRequestQuotation");
                mapAttrItem = new LinkedHashMap<String, String>();

                if (srvCurePlan.getRequestQuotation() != null && srvCurePlan.getRequestQuotation() == true) {
                    mapAttrItem.put("checked", "checked");
                } else {
                    mapRemoveAttr.put("#chkRequestQuotation", "checked");
                }
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlHotel option[value=" + intHotel + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlSpecialist option[value=" + intSpecialist + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);
                // </editor-fold>
            } else if (srvCurePlan.getStatus().equals("F")) { //สถานะ เสร็จสิ้น แผนการนัดหมาย
                //Set Message By Status
                strMessage = "<div class=\"download-box\"><p class=\"icon\"><img src=\"http://osc4.template-help.com/wt_32136/images/icon-download.png\" alt=\"\"></p>Finish your plan.<br/></div>";

                // <editor-fold defaultstate="collapsed" desc="Bind Html(innerHtml) : replace html to #'Selector'">
                mapHtml = new LinkedHashMap<String, String>();
                mapHtml.put("#message", strMessage);
                //For srvCurePlan
                mapHtml.put("#lblSymptom", srvCurePlan.getSymptom());
                mapHtml.put("#lblAllergicHistory", srvCurePlan.getAllergicHistory());
                mapHtml.put("#lblDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapHtml.put("#lblRoomType", srvCurePlan.getRoomType());
                mapHtml.put("#lblPriceMinAccept", strPriceMinAccept);
                mapHtml.put("#lblPriceMaxAccept", strPriceMaxAccept);
                mapHtml.put("#lblDateMinAccept", strDateMinAccept);
                mapHtml.put("#lblDateMaxAccept", strDateMaxAccept);
                mapHtml.put("#lblHotel", strHotel);
                mapHtml.put("#lblSpecialist", strSpecialist);
                //For srvCurePlan_doctor
                mapHtml.put("#lblSymptomComment", strSymptomComment);
                mapHtml.put("#lblAllergicHistoryComment", strAllergicHistoryComment);
                mapHtml.put("#lblDiseaseHistoryComment", strDiseaseHistoryComment);
                mapHtml.put("#lblAttachHistoryComment", strAttachHistoryComment);
                mapHtml.put("#lblRoomTypeComment", strRoomTypeComment);
                mapHtml.put("#lblPriceMinAcceptComment", strPriceMinAcceptComment);
                mapHtml.put("#lblPriceMaxAcceptComment", strPriceMaxAcceptComment);
                mapHtml.put("#lblDateMinAcceptComment", strDateMinAcceptComment);
                mapHtml.put("#lblDateMaxAcceptComment", strDateMaxAcceptComment);
                mapHtml.put("#lblRequestQuotation", strRequestQuotation);
                mapHtml.put("#lblHotelComment", strHotelComment);
                mapHtml.put("#lblSpecialistComment", strSpecialistComment);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Val : most use for bind value from DB to HtmlControl(.value)">
                mapVal = new LinkedHashMap<String, String>();
                //For Hidden Field
                mapVal.put("#curePlanId", srvCurePlan.getId().toString());
                mapVal.put("#cureServiceId", srvCurePlan.getSrvCureservice().getId().toString());
                //For srvCurePlan
                mapVal.put("#txtSymptom", srvCurePlan.getSymptom());
                mapVal.put("#txtAllergicHistory", srvCurePlan.getAllergicHistory());
                mapVal.put("#txtDiseaseHistory", srvCurePlan.getDiseaseHistory());
                mapVal.put("#txtRoomType", srvCurePlan.getRoomType());
                mapVal.put("#txtPriceMinAccept", strPriceMinAccept);
                mapVal.put("#txtPriceMaxAccept", strPriceMaxAccept);
                mapVal.put("#datDateMinAccept", strDateMinAccept);
                mapVal.put("#datDateMaxAccept", strDateMaxAccept);
                mapVal.put("#curePlanId", srvCurePlan.getId().toString());
                mapVal.put("#cureServiceId", srvCurePlan.getSrvCureservice().getId().toString());
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Visible of Control : Show/Hide Control by '1' = show, '0' = hide">
                mapShowHide = new LinkedHashMap<String, String>();
                //Show Control
                mapShowHide.put("#lblSymptom", "1");
                mapShowHide.put("#lblAllergicHistory", "1");
                mapShowHide.put("#lblDiseaseHistory", "1");
                mapShowHide.put("#lblRoomType", "1");
                mapShowHide.put("#lblPriceMinAccept", "1");
                mapShowHide.put("#lblPriceMaxAccept", "1");
                mapShowHide.put("#lblDateMinAccept", "1");
                mapShowHide.put("#lblDateMaxAccept", "1");
                mapShowHide.put("#lblHotel", "1");
                mapShowHide.put("#lblSpecialist", "1");
                //Hide Control
                mapShowHide.put("#txtSymptom", "0");
                mapShowHide.put("#txtAllergicHistory", "0");
                mapShowHide.put("#txtDiseaseHistory", "0");
                mapShowHide.put("#pnlAttachHistory", "0");
                mapShowHide.put("#txtRoomType", "0");
                mapShowHide.put("#txtPriceMinAccept", "0");
                mapShowHide.put("#txtPriceMaxAccept", "0");
                mapShowHide.put("#datDateMinAccept", "0");
                mapShowHide.put("#datDateMaxAccept", "0");
                //mapShowHide.put("#ddlHotel", "0");
                //mapShowHide.put("#ddlSpecialist", "0");
                mapShowHide.put("#uniform-ddlHotel", "0");
                mapShowHide.put("#uniform-ddlSpecialist", "0");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove : Remove 'Selector' And Can make Condition">
                mapRemove = new LinkedHashMap<String, String>();
                mapRemove.put("#uniform-btnReset", "");
                mapRemove.put("#uniform-btnSaveDraft", "");
                mapRemove.put("#uniform-btnSubmit", "");
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Remove Attributes">
                mapRemoveAttr = new LinkedHashMap<String, String>();
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="Bind Add attribute for control : use for attribute checked, disabled or else">
                //Add Attributes
                mapAttrs = new ArrayList<jQueryFormSelector>();
                attr = new jQueryFormSelector();
                attr.setId("#chkRequestQuotation");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("disabled", "disabled");
                if (srvCurePlan.getRequestQuotation() != null && srvCurePlan.getRequestQuotation() == true) {
                    mapAttrItem.put("checked", "checked");
                } else {
                    mapRemoveAttr.put("#chkRequestQuotation", "checked");
                }
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlHotel option[value=" + intHotel + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#ddlSpecialist option[value=" + intSpecialist + "]");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("selected", "selected");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);

                attr = new jQueryFormSelector();
                attr.setId("#chkAcceptCondition");
                mapAttrItem = new LinkedHashMap<String, String>();
                mapAttrItem.put("disabled", "disabled");
                mapAttrItem.put("checked", "checked");
                attr.setMapAttr(mapAttrItem);
                mapAttrs.add(attr);
                // </editor-fold>
            } else {
                //@TODO : Throw Exception message error
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="Build Json String">
            objJQueryFormFormatterUtil = new jQueryFormFormatterUtil();
            strJson = objJQueryFormFormatterUtil.setHtml(mapHtml).setVal(mapVal).setAddClass(mapAddClass).setShowHide(mapShowHide).setRemove(mapRemove).setAttr(mapAttrs).buildJson();

            return strJson;
            //ฟังก์ชันเดิมอาจไม่ใช้แล้ว
            //return jQueryFormFormatterUtil.getJSON(mapAttrs, mapRemoveAttr, mapHtml, mapText, mapVal, mapAddClass, mapRemoveClass, mapToggleClass, mapShowHide);
            // </editor-fold>
        } catch (Exception ex) {
            //@TODO : return error by throw exception
            return strJson;
        } finally {
            //@TODO : Clear Value
            // <editor-fold defaultstate="collapsed" desc="Clear Value">
            srvCurePlan = null;
            strJson = "";
            // </editor-fold>
            //@TODO : Implement message function to show on front
        }

    }
    // </editor-fold>
}
